using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Net;
using System.Net.Browser;
using System.Threading;
using App.Business.Party;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using RrslLite.Configuration;
using RrslLite.Dsl;
using RrslLite.Net.Attributes;
using RrslLite.Net.Messages;

namespace App.Test.Silverlight
{
    [TestClass]
    public class BatchFactory_Duplex_TEST
    {

        [TestInitialize]
        public void Init()
        {
            RrslLiteConfiguration.Instance.WithDefaultBatchBuilderConfiguration();

            RrslLiteConfiguration.Instance.BatchBuilderConfiguration
                .WithDuplexEndpointConfigurationName("pollingDuplex_IWcfDuplexMessageService");

            RrslLiteConfiguration.Instance.BatchBuilderConfiguration
                .WithCreateClassProxyWithTargetRecursiveToReceiveMessages()
                .WithINotifyPropertyChangedInterceptorToReceiveMessages();

            RrslLiteConfiguration.Instance.BatchBuilderConfiguration
                .EntityAopProxyGeneratorConfiguration
                .WithTargetCollectionType(typeof(ObservableCollection<>));

            RrslLiteConfiguration.Instance.BatchBuilderConfiguration
                .BatchCallBuilderConfiguration
                .WithMessageCreator(
                    (correlationId) => new Message(correlationId) { }
                );
            ;
            KnownTypeProvider.RegisterModelDomainNamespace("App.Business.Entities");
        }

        [TestMethod]
        public void BatchFactoryAsync_Using_Duplex()
        {
            IBatchBuilder factory = RrslLiteConfiguration.Instance.BatchBuilderConfiguration.GetDuplexBatchBuilder();

            factory
                .Call<IPersonFacade>()
                .WithQuery(f => f.GetActivePersonsFor("cz"), (list) =>
                                                                 {

                                                                 })
                .Execute();
        }

        [TestMethod]
        public void BatchFactoryAsync_Using_Duplex_JustSubscribe()
        {
            HttpWebRequest.RegisterPrefix("http://", WebRequestCreator.ClientHttp);

            RrslLiteConfiguration.Instance.WithDefaultBatchBuilderConfiguration();

            RrslLiteConfiguration.Instance.BatchBuilderConfiguration
                .WithDuplexEndpointConfigurationName("pollingDuplex_IWcfDuplexMessageService");

            RrslLiteConfiguration.Instance.BatchBuilderConfiguration
                .WithCreateClassProxyWithTargetRecursiveToReceiveMessages()
                .WithINotifyPropertyChangedInterceptorToReceiveMessages();

            RrslLiteConfiguration.Instance.BatchBuilderConfiguration
                .EntityAopProxyGeneratorConfiguration
                .WithTargetCollectionType(typeof(ObservableCollection<>));

            RrslLiteConfiguration.Instance.BatchBuilderConfiguration
                .BatchCallBuilderConfiguration
                .WithMessageCreator(
                    (correlationId) => new Message(correlationId) { }
                );

            int responseCounter1 = 1;
            int responseCounter2 = 1;

            IBatchBuilder factory = RrslLiteConfiguration.Instance.BatchBuilderConfiguration.GetDuplexBatchBuilder();

            Debug.WriteLine(">>> sending first batch");

            for (int i = 0; i < 200; i++)
            {
                //Thread.Sleep(100);

                factory.Call<IPersonFacade>()
                    .WithCommand(f => f.ShortRunning(), () => { Debug.WriteLine("first batch index: " + responseCounter1++); })
                    .Execute();
            }

            //factory = RrslLiteConfiguration.Instance.BatchBuilderConfiguration.GetDuplexBatchBuilder();

            Debug.WriteLine("--- Finished first batch");

            Thread.Sleep(10000);

            factory = RrslLiteConfiguration.Instance.BatchBuilderConfiguration.GetDuplexBatchBuilder();

            Debug.WriteLine(">> sending Second batch");

            for (int i = 0; i < 100; i++)
            {
                factory.Call<IPersonFacade>()
                    .WithCommand(f => f.ShortRunning(), () => { Debug.WriteLine("second batch index: " + responseCounter2++); })
                    .Execute();
            }

        }  
    }
}